package mju.cxf.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;
import mju.cxf.dto.CreditDTO;
import mju.cxf.result.Result;
import mju.cxf.service.CreditService;
import mju.cxf.vo.CreditVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 信用分相关接口
 */

@RestController
@RequestMapping("/admin/api/credit")
@Api(tags = "信用分接口列表")
public class CreditController {
    @Autowired
    private CreditService creditService;

    /**
     * 新建信用分信息
     *
     * @param creditDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新建信用分")
    public Result<String> createCredit(@RequestBody CreditDTO creditDTO) {
        return creditService.saveCredit(creditDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 修改信用分信息
     *
     * @param creditDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改信用分信息")
    public Result<String> updateCredit(@RequestBody CreditDTO creditDTO) {
        return creditService.updateCredit(creditDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 删除信用分信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除信用分信息", notes = "管理员通过此接口删除信用分信息")
    public Result<String> deleteCredit(@PathVariable Integer id) {
        return creditService.deleteCredit(id) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }



    /**
     * 分页查询信用分信息
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    @ApiOperation(value = "分页查询信用分信息", notes = "管理员通过此接口分页查询信用分信息")
    public Page<CreditVO> selectPage(@RequestParam(value = "score",required = false) Integer score,
                                     @RequestParam(value = "status",required = false) Integer status,
                                     @RequestParam(value = "current",defaultValue = "1") int current,
                                     @RequestParam(value = "size",defaultValue = "10") int size) {
        return creditService.selectPage(score, status,current, size);
    }
}
